package com.common.base.widget.popup

import android.annotation.SuppressLint
import android.app.Activity
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.common.base.R
import com.common.base.app.recycler.decorate.DividerItemDecoration
import com.common.base.utils.ViewUtils
import com.common.base.utils.ViewUtils.getColor

/**
 * 列表Popup
 */
class RecyclerPopupWindow<T> : BasePopupWindow {

    private var recyclerView: RecyclerView? = null
    private var color: Int = 0
    private var selectColor: Int = 0
    private var positionColor: Int = -1
    private var adapter: BaseQuickAdapter<T, BaseViewHolder>? = null
    private var dividerColor: Int = getColor(R.color.color_F4F4F4)
    private var dividerHeight: Int = ViewUtils.getScreenAdapterDimenPx(R.dimen.dp_1).toInt()
    private var dividerPaddingLeft: Int = 0
    private var dividerPaddingRight: Int = 0
    private var showDecoration = true

    //列表项的高度
    private var itemHeight = 0

    constructor(
        context: Activity?,
        data: List<String>,
        colorRes: Int = 0,
        positionColor: Int = -1,
        layoutRes: Int = R.layout.base_popup_recycler,
        needSetAlpha: Boolean = true
    ) : super(context, layoutRes, needSetAlpha) {
        this.color = colorRes
        this.positionColor = positionColor
        val adapter = PopupAdapter()
        adapter.addData(data)
        this.adapter = adapter as BaseQuickAdapter<T, BaseViewHolder>
        initViews()
    }

    constructor(
        context: Activity?,
        data: List<String>,
        colorRes: Int = 0,
        positionColor: Int = -1,
        layoutRes: Int = R.layout.base_popup_recycler,
        needSetAlpha: Boolean = true,
        dividerColor: Int = getColor(R.color.color_F4F4F4),
        dividerHeight: Int = ViewUtils.getScreenAdapterDimenPx(R.dimen.dp_1).toInt(),
        paddingLeft: Int = 0,
        paddingRight: Int = 0
    ) : super(context, layoutRes, needSetAlpha) {
        this.color = colorRes
        this.positionColor = positionColor
        val adapter = PopupAdapter()
        adapter.addData(data)
        this.adapter = adapter as BaseQuickAdapter<T, BaseViewHolder>
        this.dividerColor = dividerColor
        this.dividerHeight = dividerHeight
        this.dividerPaddingLeft = paddingLeft
        this.dividerPaddingRight = paddingRight
        initViews()
    }

    constructor(
        context: Activity?,
        data: List<String>,
        colorRes: Int = 0,
        positionColor: Int = -1,
        layoutRes: Int = R.layout.base_popup_recycler,
        needSetAlpha: Boolean = true,
        dividerColor: Int = getColor(R.color.color_F4F4F4),
        dividerHeight: Int = ViewUtils.getScreenAdapterDimenPx(R.dimen.dp_1).toInt(),
        paddingLeft: Int = 0,
        paddingRight: Int = 0,
        popWidth: Int = ViewGroup.LayoutParams.MATCH_PARENT,
        popHeight: Int = ViewGroup.LayoutParams.MATCH_PARENT
    )
            : super(context, layoutRes, needSetAlpha, popWidth, popHeight) {
        this.color = colorRes
        this.positionColor = positionColor
        val adapter = PopupAdapter()
        adapter.addData(data)
        this.adapter = adapter as BaseQuickAdapter<T, BaseViewHolder>
        this.dividerColor = dividerColor
        this.dividerHeight = dividerHeight
        this.dividerPaddingLeft = paddingLeft
        this.dividerPaddingRight = paddingRight
        initViews()
    }

    constructor(
        context: Activity?,
        adapter: BaseQuickAdapter<T, BaseViewHolder>,
        dividerColor: Int = getColor(R.color.color_F9F9FD),
        dividerHeight: Int = ViewUtils.getScreenAdapterDimenPx(R.dimen.dp_1).toInt(),
        colorRes: Int = 0,
        positionColor: Int = -1,
        layoutRes: Int = R.layout.base_popup_recycler,
        needSetAlpha: Boolean = true,
        showDecoration: Boolean = true,
        popWidth: Int = ViewGroup.LayoutParams.MATCH_PARENT,
        popHeight: Int = ViewGroup.LayoutParams.MATCH_PARENT
    )
            : super(context, layoutRes, needSetAlpha, popWidth, popHeight) {
        this.color = colorRes
        this.dividerColor = dividerColor
        this.dividerHeight = dividerHeight
        this.showDecoration = showDecoration
        this.positionColor = positionColor
        this.adapter = adapter
        initViews()
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun initViews() {
        recyclerView = findView(R.id.recyclerView)
        recyclerView?.layoutManager = LinearLayoutManager(context)
        if (showDecoration) {
            recyclerView?.addItemDecoration(
                DividerItemDecoration(
                    dividerColor,
                    dividerHeight,
                    paddingLeft = dividerPaddingLeft,
                    paddingRight = dividerPaddingRight
                )
            )
        }
        recyclerView?.adapter = adapter
        findView<TextView>(R.id.tvCancel)?.setOnClickListener { dismiss() }
        val rlbg = findView<View>(R.id.rlbg)
        rlbg?.setOnTouchListener { _, _ ->
            dismiss()
            false
        }
        adapter?.data?.let {
            if (it.size >= 6) {
                val params = recyclerView?.layoutParams as LinearLayout.LayoutParams
                params.height = ViewUtils.dp2px(300)
            }
        }
    }

    fun setTitleGONE() {
        findView<LinearLayout>(R.id.llTitle)?.visibility = View.GONE
    }

    fun setTitle(str: String) {
        findView<TextView>(R.id.tvTitle)?.text = str
    }

    fun setTitleColor() {
        findView<TextView>(R.id.tvTitle)?.setTextColor(getColor(R.color.color_999999))
    }

    fun setColor(color: Int, position: Int) {
        this.color = color
        this.positionColor = position
    }

    fun setPosition(position: Int) {
        this.positionColor = position
    }

    fun setColor(color: Int) {
        this.color = color
    }

    fun setSelectColor(color: Int) {
        this.selectColor = color
    }

    private var mListener: ((position: Int) -> Unit)? = null

    fun setOnItemClickListener(listener: (position: Int) -> Unit) {
        mListener = listener
    }

    fun setItemHeight(itemHeight: Int) {
        this.itemHeight = itemHeight
    }

    inner class PopupAdapter : BaseQuickAdapter<String, BaseViewHolder>(R.layout.base_popup_item) {
        override fun convert(holder: BaseViewHolder, item: String) {
            holder.apply {
                if (color != 0) {
                    if (positionColor >= 0 && positionColor == adapterPosition) {
                        setTextColor(R.id.textView, selectColor)
                    } else {
                        setTextColor(R.id.textView, color)
                    }
                }
                setText(R.id.textView, item)
                val textView = getView<TextView>(R.id.textView)
                if (itemHeight != 0) {
                    textView.layoutParams.height = itemHeight
                }
                textView.setOnClickListener {
                    mListener?.invoke(adapterPosition)
                    dismiss()
                }
            }
        }
    }

    fun notifyAdapter() {
        adapter?.notifyDataSetChanged()
    }
}